﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest2.aspx.cs" Inherits="HSchSchedule.TestPages.AjaxTest2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script src="../Scripts/jquery.min.js" type="text/javascript"></script>

    <script src="../Scripts/jquery-ui.min.js" type="text/javascript"></script>

    <script src="../Scripts/jquery-ui-i18n.min.js" type="text/javascript"></script>

    <script src="../Scripts/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>

    <script type="text/javascript" src="../JQuery/HtmlAPI.js"></script>

    <script type="text/javascript" src="../JQuery/Biz.js"></script>

    <script type="text/javascript" src="../JSClass/Entities.js"></script>

    <link href="../Css/jquery-ui.css" type="text/css" rel="Stylesheet" />
    <link href="../Css/ui.theme.css" type="text/css" rel="Stylesheet" />
    <link href="../Css/parseTheme.css.php.css" type="text/css" rel="Stylesheet" />
    <link href="../Css/style.css" type="text/css" rel="Stylesheet" />
    <link href="../Css/style1.css" type="text/css" rel="Stylesheet" />

    <script language="javascript">
        $(document).ready(function() {
            var tUrl = document.URL;
            var tArr = tUrl.split("/");
            var rootUrl = "";
            for (var i = 0; i < tArr.length - 2; i++) {
                rootUrl += tArr[i] + "/";
            }
            var ajaxAddSessionLessionUrl = rootUrl + "AjaxPages/AjaxAddSessionLession.aspx";
            var sessionUrl = rootUrl + "InputPage/SessionLessionPage.aspx";
            //Form nhập thông tin ca học trong một ngày
            var diaSessionLoad = false;
            var diaSession;
            diaSession = $("#diaSession").load(sessionUrl).dialog({
                autoOpen: false,
                modal: true,
                width: 400,
                height: 480
            });
            diaProgram.dialog('open');
            //Bind dữ liệu
            var lstSubject;
            var lstJPF;
            var cProgramId;
            var cProgram;
            var cDetails;
            var dataOk;
            $("#diaProgram #btnEdit").attr("disabled", "disabled");
            $("#diaProgram #btnCancelE").attr("disabled", "disabled");
            lstSubject = GetData(ajaxProgram + "/GetSubject");
            $("#diaProgram #tblProgram tr").remove();
            var colData = new Array();
            for (var i = 0; i < lstSubject.length; i++) {
                colData.push(lstSubject[i].SubjectName);
            }
            //$("#diaProgram #tblSubject tr").remove();
            //tableAddCol("diaProgram #tblSubject", "Môn học", colData);
            tableDelAllRow("diaProgram #tblSubject");
            var firstCol = new Array();
            //firstCol.push("Môn");
            for (var i = 0; i < lstSubject.length; i++) {
                var valArr = new Array();
                valArr.push(lstSubject[i].SubjectName);
                valArr.push('<input type="text" id="txtNum' + lstSubject[i].SubjectID + '" class="txtNumLession" value="1" />');
                valArr.push('<input type="text" id="txtCom' + lstSubject[i].SubjectID + '" class="txtComment" />');
                tableAddRow("diaProgram #tblSubject", valArr);
                firstCol.push(lstSubject[i].SubjectName);
            }
            var numCol = $("#diaProgram #tblProgram tr:eq(0) td").length;
            while (numCol > 1) {
                tableDelCol("diaProgram #tblProgram", numCol - 1);
                numCol = $("#diaProgram #tblProgram tr:eq(0) td").length;
            }
            tableAddCol("diaProgram #tblProgram", "Môn", firstCol);

            function ResetAll() {
                $("#diaProgram #btnEdit").attr("disabled", "disabled");
                $("#diaProgram #btnCancelE").attr("disabled", "disabled");
                $("#diaProgram #btnAdd").attr("disabled", "");
                $("#diaProgram #btnNext").attr("disabled", "");
                $("#diaProgram #txtProgram").val("");
                $("#diaProgram #txtComment").val("");
                $("#diaProgram #txtNumLession").val("");
                $("#diaProgram input[class=txtNumLession]").val("1");
                $("#diaProgram input[class=txtComment]").val("");
                cProgramId = null;
            }
            function LoadToEdit() {
                var i = 0;
                for (i = 0; i < lstJPF.length; i++) {
                    if (lstJPF[i].Pro.ProgramID == cProgramId) {
                        break;
                    }
                }
                //alert(cProgramId);
                $("#diaProgram #txtProgram").val(lstJPF[i].Pro.ProgramName);
                $("#diaProgram #txtComment").html(lstJPF[i].Pro.Comment);
                $("#diaProgram input[name=txtNumLession]").val("0");
                for (var j = 0; j < lstJPF[i].LstSubject.length; j++) {
                    $("#diaProgram #txtNum" + lstJPF[i].LstSubject[j].SubjectID).val(lstJPF[i].LstNumLession[j]);
                    $("#diaProgram #txtCom" + lstJPF[i].LstSubject[j].SubjectID).val(lstJPF[i].LstComment[j]);
                }
                $("#diaProgram #btnEdit").attr("disabled", "");
                $("#diaProgram #btnCancelE").attr("disabled", "");
                $("#diaProgram #btnAdd").attr("disabled", "disabled");
                $("#diaProgram #btnNext").attr("disabled", "disabled");
                $("#diaProgram #btnCancelE").click(function() {
                    ResetAll();
                });

            }
            function prepareData() {
                //Check data
                var pName = $("#diaProgram #txtProgram").val();
                pName = pName.trim();
                dataOk = true;
                if (pName.length == 0) {
                    dataOk = false;
                }
                if (dataOk) {
                    cProgram = new JProgram();
                    cProgram.ProgramName = pName;
                    cProgram.Comment = $("#diaProgram #txtComment").val();
                    cDetails = new Array();
                    $("#diaProgram #tblSubject .txtNumLession").each(function(i, select) {
                        var v = $(select).val();
                        var c = "";
                        var id = $(select).attr('id');
                        if (isNaN(v)) {
                            alert("Thông tin số tiết của môn học là số.");
                            dataOk = false;
                        }
                        else {
                            v = parseInt(v);
                            if (v > 0) {
                                var sId = id.substring(6, id.length);
                                c = $("#diaProgram #tblSubject #txtCom" + sId).val();
                                var dt = new JProgramDetail();
                                dt.SubjectID = sId;
                                dt.NumLession = v;
                                dt.Comment = c;
                                cDetails.push(dt);
                            }
                        }
                    });
                }
            }
            function LoadData() {
                lstJPF = GetData(ajaxProgram + "/GetProgram");
                alert(JSON.stringify(lstJPF));
                //Bind lên bảng các Program
                //Xóa dữ liệu cũ
                var numCol = $("#diaProgram #tblProgram tr:eq(0) td").length;
                while (numCol > 1) {
                    tableDelCol("diaProgram #tblProgram", numCol - 1);
                    numCol = $("#diaProgram #tblProgram tr:eq(0) td").length;
                }
                for (var i = 0; i < lstJPF.length; i++) {
                    var header = lstJPF[i].Pro.ProgramName + '(<a href="' + lstJPF[i].Pro.ProgramID + '" title="Edit" class="edit">Sửa </a>--<a href="' + lstJPF[i].Pro.ProgramID + '" title="Del" class="del">Xóa </a>)';
                    var colData = new Array();
                    for (var j = 0; j < lstSubject.length; j++) {
                        var k = 0;
                        for (k = 0; k < lstJPF[i].LstSubject.length; k++) {
                            if (lstSubject[j].SubjectID == lstJPF[i].LstSubject[k].SubjectID) {
                                colData.push(lstJPF[i].LstNumLession[k]);
                                break;
                            }
                        }
                        if (k >= lstJPF[i].LstSubject.length) {
                            colData.push(0);
                        }
                    }
                    tableAddCol("diaProgram #tblProgram", header, colData);
                }
                //alert("check a");
                //var numA = $("#diaProgram #tblProgram .edit").length;
                //alert(numA);
                $("#diaProgram #tblProgram .edit").click(function(event) {
                    //alert("head of function");
                    var t = $(event.currentTarget).attr('href');
                    cProgramId = t;
                    try {
                        LoadToEdit();
                    }
                    catch (err) {
                        alert(err);
                    }
                    //alert(t);
                    return false;
                });
                //alert("check d");
                $("#diaProgram #tblProgram .del").click(function(event) {
                    try {
                        var t = $(event.currentTarget).attr('href');
                        cProgramId = t;
                        if (confirm("Bạn thực sự muốn xóa chương trình học này?")) {
                            //Del chương trình học này đi
                            var nameArr = new Array();
                            nameArr.push("programId");
                            var valArr = new Array();
                            valArr.push(cProgramId);
                            var msg = GetDataPara(ajaxProgram + "/DelProgram", nameArr, valArr);
                            if (msg == "SUCCESS" || msg == null) {
                                alert("Xóa thành công!");
                            }
                            else {
                                alert("Có lỗi!");
                            }
                            ResetAll();
                            LoadData();
                        }
                    }
                    catch (err) {
                        alert(err);
                    }
                    return false;
                });
            }
            if (!diaProgramLoad) {
                $("#diaProgram #btnAdd").click(function() {
                    prepareData();
                    if (dataOk) {
                        //Them vao CSDL
                        var nameArr = new Array();
                        nameArr.push("jProgram");
                        nameArr.push("jDetails");
                        var valArr = new Array();
                        valArr.push(cProgram);
                        valArr.push(cDetails);
                        var msg = GetDataPara(ajaxProgram + "/AddProgram", nameArr, valArr);
                        if (msg == "SUCCESS" || msg == null) {
                            alert("Thành công!");
                            ResetAll();
                            LoadData();
                        }
                        else {
                            alert(msg);
                            ResetAll();
                            LoadData();
                        }
                        dataOk = false;
                    }
                    else {
                        alert("Xin vui lòng nhập đủ thông tin!");
                    }
                });
                $("#diaProgram #btnEdit").click(function() {
                    prepareData();
                    if (dataOk) {
                        var nameArr = new Array();
                        nameArr.push("currentProgram");
                        nameArr.push("jProgram");
                        nameArr.push("jDetails");
                        var valArr = new Array();
                        valArr.push(cProgramId);
                        valArr.push(cProgram);
                        valArr.push(cDetails);
                        var msg = GetDataPara(ajaxProgram + "/UpdateProgram", nameArr, valArr);
                        if (msg == "SUCCESS" || msg == null) {
                            alert("Thành công!");
                        }
                        else {
                            alert("Lỗi: " + msg);
                        }
                        LoadData();
                    }
                    else {
                        alert("Vui lòng nhập đủ thông tin");
                    }
                });

                $("#diaProgram #btnNext").click(function() {
                    if (confirm("Sang bước tiếp?")) {
                        diaProgram.dialog('close');
                    }
                });
                diaProgramLoad = true;
            }
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>
    <div id="diaSession">
    </div>
</body>
</html>
